<?php

class SoaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	//redirect to overview 
        $this->_redirect('soa/view');
    }

    public function addAction()
    {
    	$form = new Application_Form_SoaAdd();
    	
    	
    	$request = $this->getRequest();
    	
    	if($request->isPost()) {		  		
			if ($form->isValid($request->getPost())) {
				
				$soa = new Application_Model_Soa();
				$soaMapper = new Application_Model_SoaMapper();
				
				//fill SOA Model
				$soa->setCustomerNumber($form->getValue('customerNumber'));
				$soa->setBudgetCategory($form->getValue('budgetCategory'));
				$soa->setCampaignDesc($form->getValue('campaignDesc'));
				$soa->setEndDate($form->getValue('endDate'));
				$soa->setStartDate($form->getValue('startDate'));
				$soa->setCreatedBy($form->getValue('createdBy'));
				$soa->setCreatedOn($form->getValue('createdOn'));
				$soa->setStatus($form->getValue('status'));
				
				//add soa campiagn into database
				$soaMapper->add($soa);
				
				//get inserted id of campaign and forward to mask for adding items
				$lastId = Zend_Db_Table::getDefaultAdapter()->lastInsertId('tbl_soaCampaign', 'id');
								$this->_redirect('soa/additems/campaignid/'.$lastId);
			}
		}
		$this->view->pageTitle = 'Create SOA campaign';
        $this->view->form = $form;
    }

    public function viewAction()
    {
        // action body
    }

    public function editAction()
    {
        // action body
    }

    public function getcustomerformAction()
    {
    	$request = $this->getRequest();
    	$salesrep = $request->getParam('salesrepname');
    	$customerDb = new Application_Model_DbTable_Customer();
    	$data = $customerDb->getCustomerBySalesrep($salesrep);
    	$this->_helper->json($data);
    }

    public function additemsAction()
    {
        
        
        $request = $this->getRequest();
        
        $campaign_id = $request->getParam('campaignid');
        $soaMapper = new Application_Model_SoaMapper();
        // create Form
        $form = new Application_Form_SoaItemAdd();
        //set Action to correct campaignNumber
        $form->setAction('/soa/additems/campaignid/'.$campaign_id);
        $form->getElement('campaign_id')->setValue($campaign_id);

        
        
        //navigation check
        if($campaign_id != null) {
        	
        	 
        	if($request->isPost()) {
        		if ($form->isValid($request->getPost())) {
        			
        			$soaItem = new Application_Model_SoaItem();
        			$soaItem->setParentId($form->getValue('campaign_id'));
        			$soaItem->setInventoryItem($form->getValue('inventoryItem'));
        			$soaItem->setPlanQty($form->getValue('planQty'));
        			$soaItem->setPreapprovedFlag($form->getValue('preapprovedFlag'));
        			$soaItem->setPrice($form->getValue('price'));
        			$soaItem->setProcessedFlag($form->getValue('processedFlag'));
        			
        			$soaMapper->addSoaItem($soaItem);
        			
        		}
        	}
        	
        	
        	//get Campaign by submitted id and ass to view
        	$soaCampaign = $soaMapper->getSoaCampaignById($campaign_id);
        	$this->view->parentCampaign = $soaCampaign;
        	
        	//get SoaItems for campaign and pass to view
        	$soaItems = $soaMapper->getSoaItems($campaign_id);
        	$this->view->soaItems = $soaItems;
        	
        	
        	$this->view->pageTitle = 'Add items to campaign';
        	$this->view->campaign_id = $campaign_id; //debug
        	
        	 
        	$this->view->form = $form;
        } else {
        	$this->view->pageTitle = 'no campaign loaded!';
        }
        
    }
    
    public function deleteitemAction() {
    	$request = $this->getRequest();
    	$itemid = $request->getParam('itemid');
    	
    	$soaMapper = new Application_Model_SoaMapper();
    	
    	
    	$soaMapper->deleteItem($itemid);
    	$this->_helper->json('success');
    	
    }

    public function getiteminfoAction() {
    	$request = $this->getRequest();
    	//get params
    	$invItem = $request->getParam('inventoryItem');
    	$customerNbr = $request->getParam('customerNumber');
    	
    	$soaMapper = new Application_Model_SoaMapper();
    	
    	$data = $soaMapper->getViewInfo($invItem, $customerNbr);
    	
    	
    	$this->_helper->json($data);
    	
    }

}











